
package no.uib.storbioinfogui;

import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import no.uib.storbioinfogui.util.AlignedListCellRenderer;

/**
 * A simple dialog for add a new data folder.
 * 
 * @author Harald Barsnes
 */
public class NewDataFolderDialog extends javax.swing.JDialog {

    /**
     * The dataset dialog.
     */
    private NewDatasetDialog datasetDialog;
    
    /**
     * Create a new NewDataFolderDialog dialog.
     * 
     * @param datasetDialog
     * @param datasetTypes 
     * @param modal 
     */
    public NewDataFolderDialog(NewDatasetDialog datasetDialog, String[] datasetTypes, boolean modal) {
        super(datasetDialog, modal);
        this.datasetDialog = datasetDialog;
        initComponents();
        typeJComboBox.setRenderer(new AlignedListCellRenderer(SwingConstants.CENTER));
        typeJComboBox.setModel(new DefaultComboBoxModel(datasetTypes));
        setLocationRelativeTo(datasetDialog);
        rootFolderLabel.setText("\'" + datasetDialog.getStoreBioinfoGUI().getMappedFolder().getAbsolutePath() + "\' ");
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        propertiesPanel = new javax.swing.JPanel();
        folderLabel = new javax.swing.JLabel();
        folderTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        typeLabel = new javax.swing.JLabel();
        typeJComboBox = new javax.swing.JComboBox();
        rootFolderHelpLabel = new javax.swing.JLabel();
        rootFolderLabel = new javax.swing.JLabel();
        okJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        mandatoryLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Data Folder");
        setResizable(false);

        propertiesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Properties"));

        folderLabel.setText("Folder*");

        folderTextField.setEditable(false);
        folderTextField.setMargin(new java.awt.Insets(2, 4, 2, 2));

        browseButton.setText("Browse");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        typeLabel.setText("Type*");

        typeJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        rootFolderHelpLabel.setFont(rootFolderHelpLabel.getFont().deriveFont((rootFolderHelpLabel.getFont().getStyle() | java.awt.Font.ITALIC)));
        rootFolderHelpLabel.setText("Folder has to be rooted in:");

        rootFolderLabel.setFont(rootFolderLabel.getFont().deriveFont((rootFolderLabel.getFont().getStyle() | java.awt.Font.ITALIC)));
        rootFolderLabel.setText("-");

        javax.swing.GroupLayout propertiesPanelLayout = new javax.swing.GroupLayout(propertiesPanel);
        propertiesPanel.setLayout(propertiesPanelLayout);
        propertiesPanelLayout.setHorizontalGroup(
            propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(propertiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(folderLabel)
                    .addComponent(typeLabel))
                .addGap(18, 18, 18)
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(propertiesPanelLayout.createSequentialGroup()
                        .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(folderTextField)
                            .addComponent(typeJComboBox, 0, 528, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseButton))
                    .addGroup(propertiesPanelLayout.createSequentialGroup()
                        .addComponent(rootFolderHelpLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rootFolderLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        propertiesPanelLayout.setVerticalGroup(
            propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(propertiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(folderLabel)
                    .addComponent(folderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeLabel)
                    .addComponent(typeJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(propertiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rootFolderHelpLabel)
                    .addComponent(rootFolderLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        okJButton.setText("OK");
        okJButton.setEnabled(false);
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        mandatoryLabel.setFont(mandatoryLabel.getFont().deriveFont((mandatoryLabel.getFont().getStyle() | java.awt.Font.ITALIC)));
        mandatoryLabel.setText("* Mandatory fields.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(propertiesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(mandatoryLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 448, Short.MAX_VALUE)
                        .addComponent(okJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelJButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelJButton, okJButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(propertiesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelJButton)
                    .addComponent(okJButton)
                    .addComponent(mandatoryLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Close the dialog without saving.
     * 
     * @param evt 
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * Add the new data folder and close the dialog.
     * 
     * @param evt 
     */
    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed
        datasetDialog.insertDataFolder(folderTextField.getText(), (String) typeJComboBox.getSelectedItem());
        dispose();
    }//GEN-LAST:event_okJButtonActionPerformed

    /**
     * Opens a file chooser where the user can select the data folder.
     * 
     * @param evt 
     */
    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        JFileChooser fileChooser = new JFileChooser(datasetDialog.getStoreBioinfoGUI().getLastSelectedFolder());
        fileChooser.setDialogTitle("Select Data Folder");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int returnVal = fileChooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            datasetDialog.getStoreBioinfoGUI().setLastSelectedFolder(selectedFile.getParent());

            if (!selectedFile.getAbsolutePath().startsWith(datasetDialog.getStoreBioinfoGUI().getMappedFolder().getAbsolutePath())) {
                JOptionPane.showMessageDialog(this, "The datafolder has to be rooted in \'" + datasetDialog.getStoreBioinfoGUI().getMappedFolder().getAbsolutePath() + "\'.", 
                        "Datafolder Error", JOptionPane.INFORMATION_MESSAGE);
            } else {
                folderTextField.setText(selectedFile.getAbsolutePath());
                okJButton.setEnabled(true);
            } 
        }
    }//GEN-LAST:event_browseButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JLabel folderLabel;
    private javax.swing.JTextField folderTextField;
    private javax.swing.JLabel mandatoryLabel;
    private javax.swing.JButton okJButton;
    private javax.swing.JPanel propertiesPanel;
    private javax.swing.JLabel rootFolderHelpLabel;
    private javax.swing.JLabel rootFolderLabel;
    private javax.swing.JComboBox typeJComboBox;
    private javax.swing.JLabel typeLabel;
    // End of variables declaration//GEN-END:variables
}
